package com.skycong.hello77.activity.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.skycong.hello77.R;

/**
 * Created by wangqi on 18/1/11.
 */

public class CalculatorActivity extends Activity implements View.OnClickListener{

    private EditText exception;
    private Button btn9;
    private Button btn8;
    private Button btn7;
    private Button btn6;
    private Button btn5;
    private Button btn4;
    private Button btn3;
    private Button btn2;
    private Button btn1;
    private Button btn0;
    private Button add;
    private Button sub;
    private Button mul;
    private Button clr;

    private Button div;
    private Button del;
    private Button point;
    private Button calc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);
        btn0 = (Button) findViewById(R.id.btn_0);
        btn1 = (Button) findViewById(R.id.btn_1);
        btn2 = (Button) findViewById(R.id.btn_2);
        btn3 = (Button) findViewById(R.id.btn_3);
        btn4 = (Button) findViewById(R.id.btn_4);
        btn5 = (Button) findViewById(R.id.btn_5);
        btn6 = (Button) findViewById(R.id.btn_6);
        btn7 = (Button) findViewById(R.id.btn_7);
        btn8 = (Button) findViewById(R.id.btn_8);
        btn9 = (Button) findViewById(R.id.btn_9);
        mul = (Button) findViewById(R.id.multiply);
        add = (Button) findViewById(R.id.add);
        sub = (Button) findViewById(R.id.reduce);
        clr = (Button) findViewById(R.id.clear);
        div = (Button) findViewById(R.id.division);
        del = (Button) findViewById(R.id.delete);
        point = (Button) findViewById(R.id.point);
        calc = (Button) findViewById(R.id.sign);
        exception = (EditText) findViewById(R.id.edit);

        btn0.setOnClickListener(this);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        btn9.setOnClickListener(this);
        mul.setOnClickListener(this);
        add.setOnClickListener(this);
        sub.setOnClickListener(this);
        clr.setOnClickListener(this);
        div.setOnClickListener(this);
        del.setOnClickListener(this);
        point.setOnClickListener(this);
        calc.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        String str = exception.getText().toString();//获取显示器的内容
        System.out.println("你点击了" + str);
        switch (v.getId()) {
            case R.id.btn_0:
            case R.id.btn_1:
            case R.id.btn_2:
            case R.id.btn_3:
            case R.id.btn_4:
            case R.id.btn_5:
            case R.id.btn_6:
            case R.id.btn_7:
            case R.id.btn_8:
            case R.id.btn_9:
            case R.id.point:
                exception.setText(str + ((Button) v).getText());
                break;
            case R.id.add:
            case R.id.reduce:
            case R.id.multiply:
            case R.id.division:
                exception.setText(str + " " + ((Button) v).getText() + " ");//做运算，用空格隔开
                break;
            case R.id.clear:
                exception.setText("");
                break;
            case R.id.delete:
                if (str != null && !str.equals("")) {
                    System.out.println("原来的str = " + str);
                    exception.setText(str.substring(0, str.length() - 1));
                }
                break;

            case R.id.sign:
                getResult();
                break;

        }
    }

    //单独运算最后的结果
    private void getResult() {
        System.out.println("in get result");
        String edtR = exception.getText().toString();
        if (edtR == null && edtR.equals("")) {
            return;
        }
//        if(!edtR.contains(" ")){
//            return;
//        }
        Double result = 0.0;

//        String s1 = edtR.substring(0, edtR.indexOf(" "));//运算符前面的字符
//        String op = edtR.substring(edtR.indexOf(" ") + 1, edtR.indexOf(" ") + 2);//截取的运算符
//        String s2 = edtR.substring(edtR.indexOf(" ") + 3);//运算符后面的字符

//
        String op = getOpeator(exception.getText().toString());
        double d1 = getDataBeforeOP(exception.getText().toString());
        double d2 = getDataAfterOP(exception.getText().toString());
        System.out.println("data1 = " + d1);
        System.out.println("data2 = " + d2);
        System.out.println("op = " + op);
//        if (!d1.equals(" ") && !d2.equals(" ")) {
//            Double d1 = Double.parseDouble(s1);//转换成Doubel类型
//            Double d2 = Double.parseDouble(s2);


        if (op.equals("+")) {
            result = d1 + d2;
            System.out.println("我是加号");
        } else if (op.equals("-")) {
            result = d1 - d2;
        } else if (op.equals("*")) {
            result = d1 * d2;
        } else {
            result = d1 / d2;
        }
        exception.setText(result + "");
    }
//    }

    private String getOpeator(String exception) {
        if (exception == null || "".equals(exception)) return "";
        if (exception.contains("+"))
            return "+";
        if (exception.contains("-"))
            return "-";
        if (exception.contains("*"))
            return "*";
        if (exception.contains("/"))
            return "/";
        return "";
    }


    private double getDataBeforeOP(String exception) {
        String data1 = exception.substring(0, exception.indexOf(getOpeator(exception))).trim();
        return Double.parseDouble(data1);

    }

    private double getDataAfterOP(String exception) {
        String data2 = exception.substring(exception.indexOf(getOpeator(exception))+1).trim();
        return Double.parseDouble(data2);
    }
}
